package com.wxy.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.security.PublicKey;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import javax.servlet.http.Cookie;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mysql.cj.Session;
import com.wxy.domain.history;
import com.wxy.domain.transform;
import com.wxy.domain.user;
import com.wxy.service.service;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

@Controller
public class bankController {
	@Autowired
	private service service;
	@RequestMapping("/login1")										//管理登录	
	@ResponseBody
	public String login1(HttpServletRequest request) throws Exception {
		request.setCharacterEncoding("utf-8");
		String jsonString=request.getParameter("userdata");
		ObjectMapper objectMapper=new ObjectMapper();
		user user=objectMapper.readValue(jsonString,user.class);
		if(user.getUsername().equals("yinhang")&&user.getPassword().equals("yinhang123"))
		{
			return "登录成功";
		}
		else {
			return "登录失败";
		}
	}
	@RequestMapping("/login2")										//用户登录	
	@ResponseBody
	public String login2(HttpServletRequest request) throws Exception {
		request.setCharacterEncoding("utf-8");
		String jsonString=request.getParameter("userdata");
		ObjectMapper objectMapper=new ObjectMapper();
		user user=objectMapper.readValue(jsonString,user.class);
		List<user> lists=this.service.findALLUsers();
		int num=0;
		for (user user2 : lists) {
			if(user2.getUsername().equals(user.getUsername())&&user2.getPassword().equals(user.getPassword())) {
				num=1;
				break;
				}
		}
		if(num==1) {
			HttpSession session= request.getSession();
			user usermessage=this.service.findUserByUsername(user.getUsername());
			session.setAttribute("user",usermessage);
			return "登录成功";
		}
		else {
			return "登录失败";
		}
	}
	@RequestMapping("register")										//用户注册
	@ResponseBody
	public String register(HttpServletRequest request) throws Exception {
		request.setCharacterEncoding("utf-8");
		HttpSession session=request.getSession();
		String jsonString=request.getParameter("userdata");
		ObjectMapper objectMapper=new ObjectMapper();
		user user=objectMapper.readValue(jsonString, user.class);
		List<user> lists=this.service.findALLUsers();
		int isTwo=0;
		for (user user2 : lists) {
			if(user2.getUsername().equals(user.getUsername())) {
				isTwo=1;
			}
		}
		if(isTwo==1) {
			return "用户名已存在";
		}
		int num=service.findMaxId()+1;
		user.setCard_id(""+num);
		user.setMoney("0");
		service.insertUser(user);
		session.setAttribute("user",user);
		return "注册成功,您的卡id为"+num;
	}
	@RequestMapping("findAllUser")										//管理者的查看所有用户信息
	@ResponseBody
	public void test2(HttpServletResponse response) throws IOException{
		response.setContentType("application/json;charset=utf-8");
		PrintWriter printWriter=response.getWriter();
		List<user> users=this.service.findALLUsers();
		if(users.size()==0)
			printWriter.print("请添加用户");
		else {
			ObjectMapper objectMapper=new ObjectMapper();
			String jsonString=objectMapper.writeValueAsString(users);
			printWriter.print(jsonString);
		}
	}
	@RequestMapping("findAllHistory")									//管理者的查看所有交易记录
	@ResponseBody
	public List<history> test3() {
		List<history> histories=this.service.findAllHistorys();
		return histories;
	}
	@RequestMapping("findMoneyByUsername")								//用户查询余额
	@ResponseBody
	public int test4(HttpSession session) {
		user user=(com.wxy.domain.user) session.getAttribute("user");
		return this.service.findMoneyByUserName(user.getUsername());
	}
	@RequestMapping("findHistoryByCardId")							//用户查询交易记录
	@ResponseBody
	public void test5(HttpSession session,HttpServletResponse response) throws IOException{
		response.setContentType("application/json;charset=utf-8");
		PrintWriter printWriter=response.getWriter();
		ObjectMapper objectMapper=new ObjectMapper();
		user user=(com.wxy.domain.user) session.getAttribute("user");
		List<history> histories=this.service.findHistoryByCardId(user.getCard_id());
			String jsondataString=objectMapper.writeValueAsString(histories);
			printWriter.print(jsondataString);
	}
	@RequestMapping("transfer")									//转账
	@ResponseBody
	public String test6(HttpServletRequest request) throws Exception {
		request.setCharacterEncoding("utf-8");
		String jsonString=(String) request.getParameter("data");
		ObjectMapper objectMapper=new ObjectMapper();
		user user=objectMapper.readValue(jsonString, user.class);
		HttpSession session=request.getSession();
		user user2=(com.wxy.domain.user) session.getAttribute("user");
		if(!user2.getPassword().equals(user.getPassword())) {
			return "密码错误";
		}
		if(Integer.parseInt(user2.getMoney())<Integer.parseInt(user.getMoney())) {
			return "金额不足";
		}
		int i=0;
		List<user> users=this.service.findALLUsers();
		for (user user3 : users) {
			if(user3.getCard_id().equals(user.getCard_id())) {
				i=1;
				break;
			}
		}
		if(i==0)
			return "用户不存在";
		transform t1=new transform();
		t1.setMoney(user.getMoney());
		t1.setCard_id(user2.getCard_id());
		transform t2=new transform();
		t2.setMoney(user.getMoney());
		t2.setCard_id(user.getCard_id());
		String outhistoryString="您向"+user.getCard_id()+"转账了"+user.getMoney()+"元";
		String inhistoryString=user2.getCard_id()+"向您转账了"+user.getMoney()+"元";
		LocalDateTime localDateTime=LocalDateTime.now();
		DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		String timeString=dateTimeFormatter.format(localDateTime);
		history h1=new history(user2.getCard_id(), outhistoryString, timeString, "转出");
		history h2=new history(user.getCard_id(), inhistoryString, timeString, "转入");
		boolean istrue=this.service.transfer(t1, t2, h1, h2);
		user2.setMoney(Integer.toString(Integer.parseInt(user2.getMoney())-Integer.parseInt(user.getMoney())));
		session.setAttribute("user",user2);
		if(istrue==true)
			return "转账成功";
		else {
			return "转账失败";
		}
	}
	@RequestMapping("inMoney")												//存钱
	@ResponseBody
	public String test7(HttpServletRequest request) throws Exception {
		request.setCharacterEncoding("utf-8");
		HttpSession session=request.getSession();
		ObjectMapper mapper=new ObjectMapper();
		LocalDateTime localDateTime=LocalDateTime.now();
		DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		String datajsonString=request.getParameter("data");
		user user=mapper.readValue(datajsonString,user.class);
		user user2=(com.wxy.domain.user) session.getAttribute("user");
		if(!user.getPassword().equals(user2.getPassword())) {
			return "密码错误";
		}
		else {
			transform transfer=new transform(user2.getCard_id(), user.getMoney());
			String dateString=dateTimeFormatter.format(localDateTime);
			String inMoneyString="您存入了"+user.getMoney()+"元";
			history history=new history(user2.getCard_id(),inMoneyString,dateString,"存入");
			boolean b=this.service.inMoney(transfer, history);
			user2.setMoney(Integer.toString(Integer.parseInt(user2.getMoney())+Integer.parseInt(user.getMoney())));
			session.setAttribute("user",user2);
			return "存钱成功";
		}
	}
	@RequestMapping("outMoney")										//取钱
	@ResponseBody
	public String test8(HttpServletRequest request) throws Exception {
		request.setCharacterEncoding("utf-8");
		HttpSession session=request.getSession();
		ObjectMapper mapper=new ObjectMapper();
		LocalDateTime localDateTime=LocalDateTime.now();
		DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		String datajsonString=request.getParameter("data");
		user user=mapper.readValue(datajsonString,user.class);
		user user2=(com.wxy.domain.user) session.getAttribute("user");
		if(!user.getPassword().equals(user2.getPassword())) {
			return "密码错误";
		}
		if(Integer.parseInt(user.getMoney())>Integer.parseInt(user2.getMoney())) {
			return "金额不足";
		}
		else {
			transform transfer=new transform(user2.getCard_id(), user.getMoney());
			String dateString=dateTimeFormatter.format(localDateTime);
			String inMoneyString="您取出了"+user.getMoney()+"元";
			history history=new history(user2.getCard_id(),inMoneyString,dateString,"取出");
			boolean b=this.service.outMoney(transfer, history);
			user2.setMoney(Integer.toString(Integer.parseInt(user2.getMoney())-Integer.parseInt(user.getMoney())));
			session.setAttribute("user",user2);
			return "取钱成功";
		}
	}
	@RequestMapping("removeSession")
	@ResponseBody
	public String test9(HttpServletRequest request) {
		if(request.getSession(false)==null) {
			return "退出成功";
		}
		HttpSession session=request.getSession(false);
		session.invalidate();;
		return "退出成功";
	}
}
